//
//  LiveViewController.m
//  TasteEnjoy
//
//  Created by XuRui on 16/4/26.
//  Copyright © 2016年 XuRui. All rights reserved.
//

#warning 2.Live masroy 自适应

#import "LiveViewController.h"


@interface LiveViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, retain) BaseTableView *tableView;  /**< tableview */
@property (nonatomic, retain) BaseLabel *labelTitle; /**< 题目 */

@property (nonatomic, retain) NSMutableArray *arrClub;

@property (nonatomic, assign) NSInteger page;/**< 用来保存请求新数据动态值 */

@end

@implementation LiveViewController

#pragma mark - ------------ 内存 管理 ---------------
- (void)dealloc
{
 
    [_tableView release];
    [_labelTitle release];
    [_arrClub release];
    _tableView.delegate = nil;
    _tableView.dataSource = nil;
    
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.navigationItem.title = @"Topic";
    self.arrClub = [NSMutableArray array];
    [self dataHandle];
    [self createTableView];
    [self creatTitleView];
    [self refresh];
    
}
#pragma mark - ------------ 数据解析 ---------------
- (void)dataHandle
{
    
    NSString *str = @"http://203.80.144.213/napi/topic/list/by_tags/?locale=zh_CN&tags=精选&limit=25&screen_height=568&screen_width=320&start=0&platform_version=9.3.1&app_code=gandalf&device_platform=iPhone5%2C2&platform_name=iPhone%20OS&__domain=www.duitang.com&app_version=6.0.6%20rv%3A156728&device_name=iPhone%205";
    //有中文 转UTf8码
    NSString *urlStr = [str stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
    
    NSString *url = [urlStr stringByReplacingOccurrencesOfString:@"start=0" withString:[NSString stringWithFormat:@"start=%ld", (long)_page]];
    
    [XrDataParse GETWithURL:url par:nil success:^(id responseObject) {
        
        NSArray *arr = responseObject[@"data"][@"object_list"];
        NSArray *arrC =  [ClubModel modelHandleWithArrar:arr];
        [_arrClub addObjectsFromArray:arrC];
        
        [_tableView reloadData];
        
    } filed:^(NSError *error) {
        
    }];
   
}



#pragma mark - ------------ 下拉刷新 上拉加载 ---------------

- (void)refresh
{
    //刷新
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        _page = 0;
        [self dataHandle];
        [_tableView.mj_header endRefreshing];
    }];
    
    //加载
    self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        
        _page += 25;
        [self dataHandle];
        [_tableView.mj_footer endRefreshing];
    }];
    
}



#pragma mark - ------------ 热门话题 ---------------
- (void)creatTitleView
{
    
    self.labelTitle = [[BaseLabel alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 40)];
    /** 夜间 */
    _labelTitle.dk_backgroundColorPicker = DKColorWithColors([UIColor colorWithRed:0.93 green:0.93 blue:0.93 alpha:1.00],[UIColor lightGrayColor]);
    _labelTitle.text = @"    热门话题";
    _labelTitle.font = [UIFont systemFontOfSize:15];
    _labelTitle.textColor = [UIColor grayColor];
    self.tableView.tableHeaderView = _labelTitle;
    [_labelTitle release];
    
}


#pragma mark - ------------ 创建Tableview ---------------
- (void)createTableView
{
    
    self.tableView = [[BaseTableView alloc]initWithFrame:SCREEN_BOUNDS style:UITableViewStylePlain];
    [self.view addSubview:_tableView];
    
    _tableView.delegate = self;
    _tableView.dataSource = self;
/** 夜间 */
    _tableView.dk_backgroundColorPicker = DKColorWithColors([UIColor whiteColor] ,[UIColor grayColor]);
    
    [_tableView registerClass:[ClubTableViewCell class] forCellReuseIdentifier:@"photo"];
    
    [_tableView release];
}


#pragma mark - ------------ 返回个数 ---------------
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
    return _arrClub.count;
}

#pragma mark - ------------ cell赋值 ---------------

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    ClubTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"photo"];
   
    
    if (_arrClub.count != 0) {
        
        ClubModel *m = _arrClub[indexPath.row];
        
        cell.model= m;
    }
    
    return cell;
}


#pragma mark - ------------ 返回高度 ---------------
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    ClubModel *m = _arrClub[indexPath.row];
    CGFloat h = [ClubTableViewCell heightWithText:m.content];
    
    if (m.path != nil) {
        
        
        return h + 190;
    }else{
        
        
        return h + 60;
        
    }
    
}


#pragma mark - ------------ tableView 点击 传下标 ---------------
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    
    ClubDetailViewController *detail = [[ClubDetailViewController alloc]init];
    ClubModel *m = _arrClub[indexPath.item];
    detail.modelDetail = m;
    [self.navigationController pushViewController:detail animated:YES];
    [detail release];

}



#pragma mark - ------------ 将要结束拖拽 当滑动速度>0 导航栏隐藏 <0出现 ---------------
- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset
{
    //往下 3.89 往上:负值
       if (velocity.y > 0) {
        self.navigationController.navigationBar.hidden = YES;
        self.tabBarController.tabBar.hidden = YES;
        
    }else{
        
        self.navigationController.navigationBar.hidden = NO;
        self.tabBarController.tabBar.hidden = NO;
        
    }
}

#pragma mark - ------------ scrollview 最后走的方法 滑动减速停止 导航栏出现 ---------------
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    
    self.navigationController.navigationBar.hidden = NO;
    self.tabBarController.tabBar.hidden = NO;
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
